package com.chocho.producerserver.controller;

import com.chocho.producerserver.service.QCollectionService;
import com.chocho.producerserver.service.QuestionService;
import com.chocho.producerserver.utils.Msg;
import com.chocho.producerserver.vo.VCollection;
import com.chocho.producerserver.vo.VQuestion;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chocho on 2022/4/11.
 */
@CrossOrigin
@RestController
@RequestMapping("/question")
@PreAuthorize("hasAuthority('question')")
public class QuestionController {
    private Logger logger = LoggerFactory.getLogger(QuestionController.class);

    @Autowired
    private QuestionService questionService;

    @PreAuthorize("hasAuthority('question:add')")
    @PostMapping("/add")
    @ResponseBody
    public Msg addQuestion(HttpServletRequest request, @RequestBody VQuestion question){
        return questionService.addQuestion(request, question) ? Msg.success() : Msg.fail();
    }

    @PreAuthorize("hasAuthority('question:list')")
    @PostMapping("/select")
    @ResponseBody
    public Msg selectQuestion(@RequestBody Map<String, Object> map){
        List<VQuestion> questionList = questionService.getQuestionByQuery(map);
        return (questionList != null)
                ? Msg.success().add("questionList", questionList)
                : Msg.fail();
    }

    @PreAuthorize("hasAuthority('question:list')")
    @PostMapping("/select/my")
    @ResponseBody
    public Msg selectMyQuestion(HttpServletRequest request, @RequestBody Map<String, Object> map){
        List<VQuestion> questionList = questionService.getMyQuestionByQuery(request, map);
        return (questionList != null)
                ? Msg.success().add("questionList", questionList)
                : Msg.fail();
    }

    @PreAuthorize("hasAuthority('question:delete')")
    @GetMapping("/delete")
    @ResponseBody
    public Msg deleteQuestion(String ids){
        if(ids != null && !"".equals(ids)){
            return questionService.deleteQuestion(ids) ? Msg.success() : Msg.fail();
        }
        return Msg.fail();
    }

    @PreAuthorize("hasAuthority('question:update')")
    @PostMapping("/update")
    @ResponseBody
    public Msg updateQuestion(HttpServletRequest request, @RequestBody VQuestion question){
        return questionService.updateQuestion(request, question) ? Msg.success() : Msg.fail();
    }

    //计算题目数量
    @PreAuthorize("hasAuthority('question:count')")
    @GetMapping("/count")
    @ResponseBody
    public Msg countQuestion(){
        return Msg.success().add("count", questionService.count());
    }

    //查询题目测试通过状态
    @PreAuthorize("hasAuthority('question:list')")
    @GetMapping("/select/status")
    @ResponseBody
    public Msg selectTestStatus(String questionId){
        return Msg.success().add("status", questionService.getStatus(questionId));
    }
}
